package net.akkz.action.imgView;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.akkz.beanFactory.BeanFactory;
import net.akkz.model.ImgView;
import net.akkz.model.Student;
import net.akkz.server.ImgViewServer;
import net.akkz.server.StudentServer;

@WebServlet(name="ImgViewDetail", urlPatterns="/student/imgViewDetail.do")
public class ImgViewDetail extends HttpServlet
{
	private ImgViewServer imgViewServer;
	private StudentServer studentServer;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
	{
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
	{
		Student student = (Student)request.getSession().getAttribute("student");
		
		int id = Integer.parseInt(request.getParameter("id"));
		
		ImgView imgView;
		if(student == null)
			imgView = imgViewServer.getImgView(id);
		else
			imgView = imgViewServer.getImgView(id, student.getId());
		
		request.setAttribute("imgView", imgView);
		
		if(imgView != null)
			request.setAttribute("publishStudentId", studentServer.get(imgView.getStudentid()).getName());
		
		request.getRequestDispatcher("/jsp/ImgView.jsp").forward(request, response);
	}
	
	@Override
	public void init()
	{
		BeanFactory  bf = (BeanFactory) this.getServletContext().getAttribute("beanFactory");
		
		imgViewServer = bf.getBean("imgViewServer");
		studentServer = bf.getBean("studentServer");
	}
}
